<?php

/**
 * User: day
 * Date: 2018/10/9
 * Time: 9:52
 * Desc: 用户流水统计
 */

namespace App\Console\Commands\ReportData;

use Illuminate\Support\Facades\DB;
use common\utils\RedisKeys;
use common\enum\MoneyLogEnum;
use App\Models\MoneyLog;
use App\Models\ReportMoneyLog as ReportMoneyLogModel;

class ReportMoneyLog extends BaseReport
{

    protected $signature = self::PREFIX . 'report_money_log {--reload=} {--begin=} {--end=} {--command=}';

    protected $description = '用户流水统计';

    public $reportName = "ReportMoneyLog";
    /**
     * 获取需要处理的数据
     */
    public function getData($beginTime, $endTime)
    {
        return  MoneyLog::select([
            DB::raw("DATE_FORMAT(log.created_at, '%Y-%m-%d') as date"),
            'log.hall_id',
            'log.user_id',
            'log.username',
            'log.type',
            'log.amount',
            'log.coupon_amount',
            'log.fee_amount',
            'user.agent_id',
            'user.agent_name',
        ])->from('money_log as log')
            ->whereBetween('log.created_at', [$beginTime, $endTime])
            ->leftJoin('user', 'user.id', '=', 'log.user_id')
            // 只获取收入支出
            ->whereIn('log.category', [MoneyLogEnum::CATEGORY_IN, MoneyLogEnum::CATEGORY_OUT])
            // 逻辑放到代码实现，怕用数据库计算占用资源
            // ->groupBy(['date', 'user_id', 'type'])
            ->get()
            ->toArray();
    }

    /**
     * 处理数据
     */
    public function dealData($data)
    {
        $result = [];
        foreach ($data as $key => $value) {
            $res = $result[$value['date']][$value['user_id']] ?? [
                'rebet_amount' => 0,
                'deposit_online_times' => 0,
                'deposit_online_amount' => 0,
                'coupon_active_amount' => 0,
                'coupon_deposit_amount' => 0,
                'deposit_offline_times' => 0,
                'deposit_manual_times' => 0,
                'deposit_offline_amount' => 0,
                'deposit_manual_amount' => 0,
                'withdraw_online_times' => 0,
                'withdraw_manual_times' => 0,
                'withdraw_online_amount' => 0,
                'withdraw_manual_amount' => 0,
                'deposit_total_amount' => 0,
                'withdraw_fee_amount' => 0,
                'withdraw_refuse_times' => 0,
                'withdraw_refuse_amount' => 0,
            ];

            $res['hall_id'] = $value['hall_id'];
            $res['date'] = $value['date'];
            $res['user_id'] = $value['user_id'];
            $res['username'] = $value['username'];
            $res['agent_id'] = $value['agent_id'];
            $res['agent_name'] = $value['agent_name'];
            switch ($value['type']) {
                case MoneyLogEnum::USER_DEPOSIT_ONLINE:
                    //type=100
                    $res['deposit_online_times']++;     //'线上存款次数',
                    $res['deposit_online_amount'] += $value['amount'];   //'线上存款金额 单位厘',
                    $res['coupon_deposit_amount'] += $value['coupon_amount'];   //'存款优惠金额 单位厘',
                    break;
                case MoneyLogEnum::USER_DEPOSIT_COMPANY:
                    //type=101
                    $res['deposit_offline_times']++;   //'线下存款次数',
                    $res['deposit_offline_amount'] += $value['amount']; //'线下存款金额 单位厘',
                    $res['coupon_deposit_amount'] += $value['coupon_amount'];   //'存款优惠金额 单位厘',
                    break;
                case MoneyLogEnum::USER_DEPOSIT_ACTIVITY:
                    //type=102
                    $res['coupon_active_amount'] += $value['coupon_amount'];     //'活动优惠金额 单位厘',
                    break;
                case MoneyLogEnum::USER_DEPOSIT_ACTIVITY_RECHARGE:
                    //type=108
                    $res['coupon_deposit_amount'] += $value['coupon_amount'];   //'存款优惠金额 单位厘',
                    break;

                case MoneyLogEnum::USER_DEPOSIT_REBET:
                    //type=103
                    $res['rebet_amount'] += $value['coupon_amount']; //'总的返水金额 单位厘',
                    break;
                case MoneyLogEnum::USER_WITHDROW_REVOKE_REBET:
                    //type=206
                    $res['rebet_amount'] -= $value['coupon_amount']; //'减去 返水冲销金额 单位厘',
                    break;
                case MoneyLogEnum::USER_DEPOSIT_MANUAL:
                    //type=104
                    $res['deposit_manual_times']++;     //'手动存款次数',
                    $res['deposit_manual_amount'] += $value['amount'];   //'手动存款金额 单位厘',
                    $res['coupon_deposit_amount'] += $value['coupon_amount'];   //'手动存款优惠金额 单位厘',
                    break;
                case MoneyLogEnum::USER_DEPOSIT_MANUAL_REBET:
                    //type=105
                    $res['rebet_amount'] += $value['coupon_amount']; //'总的返水金额 单位厘',
                    break;
                case MoneyLogEnum::USER_DEPOSIT_MANUAL_ACTIVITY:
                    //type=106
                    $res['coupon_active_amount'] += $value['coupon_amount'];     //'活动优惠金额 单位厘',
                    break;
                case MoneyLogEnum::USER_DEPOSIT_PRIZE_ADD:
                    break;
                // case MoneyLogEnum::USER_WITHDROW_WITHDRAW:
                //     $res['withdraw_online_times']++;   //'玩家取款次数',
                //     $res['withdraw_online_amount'] += $value['amount']; //'玩家取款金额 单位厘',
                    //$res['withdraw_fee_amount'] += $value['fee_amount']; //'取款被扣费用金额 单位厘',
                   // break;
                /*case MoneyLogEnum::USER_WITHDROW_WITHDRAW_MANUAL:
                    $res['withdraw_manual_times']++;   //'厅主帮玩家-手动取款次数',
                    $res['withdraw_manual_amount'] += $value['amount']; //'厅主帮玩家-手动取款金额 单位厘',
                    break;*/
                case MoneyLogEnum::USER_CASH_WITHDRAWAL_PAYED:
                    //type=205
                    $res['withdraw_fee_amount'] += $value['fee_amount']; //'取款被扣费用金额 单位厘',
                    $res['withdraw_online_times']++;   //'玩家取款次数',
                    $res['withdraw_online_amount'] += $value['amount']; //'玩家取款金额 单位厘',
                    break;
                case MoneyLogEnum::USER_CASH_OFFLINE_WITHDRAWAL_PAYED:
                    //type=207
                    $res['withdraw_fee_amount'] += $value['fee_amount']; //'取款被扣费用金额 单位厘',
                    $res['withdraw_manual_times']++;   //'玩家取款次数',
                    $res['withdraw_manual_amount'] += $value['amount']; //'玩家取款金额 单位厘',
                    break;
                case MoneyLogEnum::USER_WITHDROW_DEDUCT_ACTIVITY:
                    //type=202
                    $res['coupon_active_amount'] -= $value['coupon_amount'];     //'活动优惠金额 单位厘',
                    break;
                case MoneyLogEnum::USER_CASH_WITHDRAWAL_REFUSED:
                    //type=109
                    $res['withdraw_refuse_times']++;     //'提现被拒绝次数',
                    $res['withdraw_refuse_amount'] += $value['amount'];     //'提现被拒绝金额 单位厘',
                    break;
                case MoneyLogEnum::USER_WITHDROW_REDUCE_MANUAL:
                    break;
                case MoneyLogEnum::USER_WITHDROW_PRIZE_REDUCE:
                    break;
                default:
                    break;
            }
            //'总的优惠金额 = 活动优惠+存款优惠 单位厘',
            $res['coupon_total_amount'] = $res['coupon_active_amount'] + $res['coupon_deposit_amount'] + $res['rebet_amount'];
            //'总存款次数 = 线上存款次数+线下存款次数+手动存款次数',
            $res['deposit_total_times'] = $res['deposit_online_times'] + $res['deposit_offline_times'] + $res['deposit_manual_times'];
            //'总存款金额 = 线上存款+线下存款+手动存款 单位厘',
            $res['deposit_total_amount'] = $res['deposit_online_amount'] + $res['deposit_offline_amount'] + $res['deposit_manual_amount'];
            //'总提款次数',
            $res['withdraw_total_times'] = $res['withdraw_online_times'] + $res['withdraw_manual_times'];
            //'总提款金额 单位厘',
            $res['withdraw_total_amount'] = $res['withdraw_online_amount'] + $res['withdraw_manual_amount'];

            $result[$value['date']][$value['user_id']] = $res;
        }

        foreach ($result as $date => $users) {
            foreach ($users as $userId => $value) {
                // update or insert
                $model = ReportMoneyLogModel::where(['hall_id' => $value['hall_id'], 'date' => $value['date'], 'user_id' => $value['user_id']])->first();
                if (empty($model)) {
                    $model = new ReportMoneyLogModel($value);
                }else{
                    $model->update($value);
                }
                if (!$model->save()) {
                    $this->error("{$this->processName} 保存数据失败: " . json_encode($model->getErrors(), JSON_UNESCAPED_UNICODE));
                    return false;
                }
            }
        }
        return true;
    }
}